
var height = 400;
height = $(window).height() -200;
$(function () {
	//企业用户
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/company/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '企业名称', name: 'companyName', index: 'company_name', width: 80 },
			{ label: '企业账号', name: 'userName', index: 'userName', width: 80,formatter:function (cellValue,options,row) {
				return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.look(\''+row.id+'\')">'+cellValue+'</a>';
			} },
			{ label: '联系人', name: 'legalPerson', index: 'legal_person', width: 80 },
			{ label: '联系人电话', name: 'mobile', index: 'mobile', width: 80 },
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 80,formatter:function (cellValue) {
				return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(cellValue))
			} },
			{ label: '状态', name: 'status', index: 'status', width: 80,formatter:function (cellValue) {

				return harCodeNameWithCss(cellValue,"","",switchJsonCss,"");
			} },
			{ label: '操作', name: 'status', index: 'status', width: 80,formatter:function (cellValue,options,row) {
				if(1== cellValue){
					return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(\''+row.id+'\',\'0\')">禁用</a>';
				}else{
					return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(\''+row.id+'\',\'1\')">启用</a>';
				}
			} }
        ],
		viewrecords: true,
        height: height,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });



	//个人用户
});




var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tCompany: {},
		tUser:{}
	},
	methods: {
		companyQuery: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tCompany = {};
		},
		update: function (rowId,status) {
            vm.getInfo(rowId);
			vm.tCompany.status = status;
			vm.saveOrUpdate();
		},
		saveOrUpdate: function (event) {
			var url = vm.tCompany.id == null ? "business/company/save" : "business/company/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.tCompany),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "business/company/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			ajax(baseURL + "business/company/info/"+id,{},"get",false,false).then(function (r) {
				vm.tCompany = r.tCompany;
			})

		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		userQuery:function () {
			vm.showList = false;

			var width = $("#jqGrid").width();//设置宽度

			$("#jqGridUser").jqGrid({
				url: baseURL + 'business/user/list',
				datatype: "json",
				colModel: [
					{ label: 'id', name: 'id', index: 'id', key: true,hidden:true },
					{ label: '真实姓名', name: 'realName', index: 'realName'},
					{ label: '用户名', name: 'userName', index: 'userName'},
					{ label: '联系人电话', name: 'mobile', index: 'mobile' },
					{ label: '身份证', name: 'idCard', index: 'idCard' },
					{ label: '就职企业', name: 'companyName', index: 'company_name' },
					{ label: '注册时间', name: 'createTime', index: 'create_time',formatter:function (cellValue) {
						return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(cellValue));
					} },
					{ label: '状态', name: 'status', index: 'status',formatter:function (cellValue) {
						return harCodeNameWithCss(cellValue,"","",switchJsonCss,"");
					}},
					{ label: '操作', name: 'status', index: 'status',formatter:function (cellValue,options,row) {
						if(1== cellValue){
							return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.updateUser(\''+row.id+'\',\'0\')">禁用</a>';
						}else{
							return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.updateUser(\''+row.id+'\',\'1\')">启用</a>';
						}
					}}
				],
				viewrecords: true,
				height: height,
				rowNum: 10,
				rowList : [10,30,50],
				rownumbers: true,
				// autowidth:true,
				width:width,
				pager: "#jqGridPagerUser",
				jsonReader : {
					root: "page.list",
					page: "page.currPage",
					total: "page.totalPage",
					records: "page.totalCount"
				},
				prmNames : {
					page:"page",
					rows:"limit",
					order: "order"
				},
				gridComplete:function(){
					//隐藏grid底部滚动条
					$("#jqGridUser").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
				}
			}).trigger("reloadGrid");
			// var page = $("#jqGridUser").jqGrid('getGridParam','page');
			// $("#jqGridUser").jqGrid('setGridParam',{
			// 	page:page
			// }).trigger("reloadGrid");
		},
		look:function (rowId) {
			vm.getInfo(rowId);
			layer.open({
				type:1,
				skin: 'layui-layer-lan',
				title: "企业信息",
				area: ['500px', '350px'],
				offset:"100px",
				scrollbar:true,
				closeBtn:1,
				content: jQuery("#companyInfo")
				});
		},
		updateUser: function (rowId,status) {
			vm.getInfoUser(rowId);
			vm.tUser.status = status;
			vm.saveOrUpdateUser();
		},
		saveOrUpdateUser: function (event) {
			var url = vm.tUser.id == null ? "business/user/save" : "business/user/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.tUser),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reloadUser();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfoUser: function(id){
			ajax(baseURL + "business/user/info/"+id,{},"get",false,false).then(function (r) {
				vm.tUser = r.tUser;
			})

		},
		reloadUser: function (event) {
			vm.showList = false;
			var page = $("#jqGridUser").jqGrid('getGridParam','page');
			$("#jqGridUser").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		},
	}
});

